package com.dyz.leetcode._14_binarySearchTree;

import com.dyz.leetcode.TreeNode;

public class _235_lowestCommonAncestor {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode ancestor = root;
        while (ancestor!=null){
            if(p.val<ancestor.val && q.val<ancestor.val){
                ancestor = ancestor.left;

            }else if(p.val>ancestor.val && q.val> ancestor.val){
                ancestor = ancestor.right;
            }else {
                break;
            }
        }
        return ancestor;
    }
}
